package cn.xinfei.xdecision.manager.config;

import cn.xinfei.xdecision.manager.web.interceptor.TokenAuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
public class XDecisionWebMvcConfigurer implements WebMvcConfigurer {

    @Autowired
    private TokenAuthenticationInterceptor tokenAuthenticationInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  // 拦截所有的请求
                .allowedOrigins("*")  // 可跨域的域名，可以为 *
                .allowCredentials(true)
                .allowedMethods("*")   // 允许跨域的方法，可以单独配置
                .allowedHeaders("*");  // 允许跨域的请求头，可以单独配置
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //  添加拦截器
        registry.addInterceptor(tokenAuthenticationInterceptor)
                //排除拦截器要拦截的路径
                .excludePathPatterns("")
                //添加拦截器需要要拦截的路径
                .addPathPatterns("/datamanage/**")
                .addPathPatterns("/v3/**")
                .addPathPatterns("/v2/sysLog/**");

    }
}
